/**
 * 김환태
 * 140424 
 */

package Stream;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;

import java.awt.Font;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class MapMain extends JFrame {

	String fileName = "NoName";
	boolean modifyFlag =false;
	
	MapInput input = null;
	MapUpdate update = null;
	MapList list = null;
	
	HashMap<String , MapData> map = new HashMap<String, MapData>(); // HashMap 선언,생성
	
	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	private JMenu mnNewMenu_1;
	private JMenuItem mntmNewMenuItem_3;
	private JMenuItem mntmNewMenuItem_4;
	private JMenuItem mntmNewMenuItem_5;
	private JMenuItem mntmNewMenuItem_6;
	private JLabel status;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MapMain frame = new MapMain();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MapMain() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 742, 626);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getStatus());
	}
	
	public void newFile()throws Exception{
		
	}
	
	public void load()throws Exception{// 모든 Exception를 던진다.
				int yn = -1;
				if (modifyFlag) {// 현재 파일의 내용이 수정된 경우
					yn = JOptionPane.showConfirmDialog(null, "파일이 변경되었습니다. 저장하시겠습니까?");
					if (yn == JOptionPane.YES_NO_OPTION)
						save(); // 예스를 눌렀을경우 save()메서드를 실행시켜 저장
				}
				// 저장된 이후이거나, 파일이 없거나..
				JFileChooser fc = new JFileChooser();
				yn = fc.showOpenDialog(this);// 버튼눌럿을때의 상수값을 yn에 저장
				if (yn == JFileChooser.APPROVE_OPTION) {// 열기버튼을 눌럿을때 APPROVE_OPTION의
														// 상수값과 같냐.(같으면 true)
					File f = fc.getSelectedFile();
					fileName = f.getPath();// getPath : 경로정보
					setTitle("파일명 : " + fileName);

					FileInputStream fr = new FileInputStream(fileName);
					ObjectInputStream br = new ObjectInputStream(fr);
					map = (HashMap/*<String,MapData>*/)br.readObject();
					
					if(input != null) input.setMap(map);
					if(update != null) update.setMap(map);
					if(list != null) list.setMap(map);
					
					br.close();
					fr.close();
				}
			
	}
	
	public void save() throws Exception{

		if (fileName.equals("NoName")){
			saveAs();// 파일이름이 NoName인경우는 한번도 저장한적이 없다는 것이기 때문에 새로 저장하는 메서드인
						// saveAs()실행
			return;
		}
		FileOutputStream fw = new FileOutputStream(fileName);
		ObjectOutputStream bw = new ObjectOutputStream(fw);

		bw.writeObject(map);
		status.setText("파일이 저장되었습니다.");

		bw.close();
		fw.close();
	
	}
	
	public void saveAs()throws Exception{
		JFileChooser fc = new JFileChooser(fileName);
		int yn = fc.showSaveDialog(this);
		if (yn == JFileChooser.APPROVE_OPTION) {
			File f = fc.getSelectedFile();
			fileName = f.getPath();
			setTitle(fileName);

			save();
		}
	
		
	}

	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu_1());
			menuBar.add(getMnNewMenu());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("자료처리");
			mnNewMenu.add(getMntmNewMenuItem());
			mnNewMenu.add(getMntmNewMenuItem_1());
			mnNewMenu.add(getMntmNewMenuItem_2());
		}
		return mnNewMenu;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("Input");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(input == null){
						input = new MapInput(map);//MapInput의 생성자로 HashMap변수 map전달
						input.setBounds(50, 50, 333, 144);
					}
					contentPane.add(input);
					input.setVisible(true);
				}
			});
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("Update");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(update == null){
						update = new MapUpdate(map);
						update.setBounds(390, 50, 313, 210);
					}
					contentPane.add(update);
					update.setVisible(true);
					
				}
			});
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("List");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(list == null){
						list = new MapList(map);
						list.setBounds(50, 205, 322, 329);
					}
					contentPane.add(list);
					list.setVisible(true);
				}
			});
		}
		return mntmNewMenuItem_2;
	}
	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("파일");
			mnNewMenu_1.add(getMntmNewMenuItem_3());
			mnNewMenu_1.add(getMntmNewMenuItem_4());
			mnNewMenu_1.add(getMntmNewMenuItem_5());
			mnNewMenu_1.add(getMntmNewMenuItem_6());
		}
		return mnNewMenu_1;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("새 파일");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						newFile();
					} catch (Exception e) {
						// TODO: handle exception
						status.setText(e.toString());
					}
				}
			});
		}
		return mntmNewMenuItem_3;
	}
	private JMenuItem getMntmNewMenuItem_4() {
		if (mntmNewMenuItem_4 == null) {
			mntmNewMenuItem_4 = new JMenuItem("읽기");
			mntmNewMenuItem_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						load();
					} catch (Exception e) {
						// TODO: handle exception
						status.setText(e.toString());
					}
				}
			});
		}
		return mntmNewMenuItem_4;
	}
	private JMenuItem getMntmNewMenuItem_5() {
		if (mntmNewMenuItem_5 == null) {
			mntmNewMenuItem_5 = new JMenuItem("저장");
			mntmNewMenuItem_5.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						save();
					} catch (Exception e) {
						// TODO: handle exception
						status.setText(e.toString());
					}
				}
			});
		}
		return mntmNewMenuItem_5;
	}
	private JMenuItem getMntmNewMenuItem_6() {
		if (mntmNewMenuItem_6 == null) {
			mntmNewMenuItem_6 = new JMenuItem("새 저장");
			mntmNewMenuItem_6.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					try {
						saveAs();
					} catch (Exception e) {
						// TODO: handle exception
						status.setText(e.toString());
					}
				}
			});
		}
		return mntmNewMenuItem_6;
	}
	private JLabel getStatus() {
		if (status == null) {
			status = new JLabel(" ");
			status.setBounds(12, 10, 320, 15);
		}
		return status;
	}
}
